package com.blqy.fr.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.blqy.fr.model.user.TeamUser;
import com.blqy.fr.vo.team.TeamListItemVo;
import com.blqy.fr.vo.user.UserMyVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface TeamUserMapper extends BaseMapper<TeamUser> {
    List<UserMyVo> selectMembersByTeamId(Long teamId);

    @Select("SELECT t.id AS teamId, t.team_name AS teamName, " +
            "CASE WHEN tu.state = 1 THEN true ELSE false END AS isCurrent " +
            "FROM team t " +
            "INNER JOIN team_user tu ON t.id = tu.team_id " +
            "WHERE tu.user_id = #{userId} " +
            "AND tu.state IN (0, 1) " +  // 排除已退出（如 state=2）
            "ORDER BY tu.state DESC, t.id DESC") // 把当前团队排前面
    List<TeamListItemVo> selectTeamListByUserId(Long userId);


    @Select("SELECT team_id FROM team_user WHERE user_id = #{userId} AND state = 1 AND is_deleted = 0 LIMIT 1")
    Long selectTeamIdByUserId(Long userId);
}